package com.hzeport.bgs22emduty.util;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.StringUtils;

import java.util.List;
@Slf4j
public class ExcelFillCellMergeStrategy implements CellWriteHandler {

    private int[] mergeColumnIndex;
    private int mergeRowIndex;

    private ExcelFillCellMergeStrategy() {
    }

    public ExcelFillCellMergeStrategy(int mergeRowIndex, int[] mergeColumnIndex) {
        this.mergeRowIndex = mergeRowIndex;
        this.mergeColumnIndex = mergeColumnIndex;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }



    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        //当前行
        int curRowIndex = cell.getRowIndex();
        //当前列
        int curColIndex = cell.getColumnIndex();

        if (curRowIndex > mergeRowIndex) {
            for (int i = 0; i < mergeColumnIndex.length; i++) {
                if (curColIndex == mergeColumnIndex[i]) {
                    mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
                    break;
                }
            }
        }
    }



    /**
     * 当前单元格向上合并
     *
     * @param writeSheetHolder
     * @param cell             当前单元格
     * @param curRowIndex      当前行
     * @param curColIndex      当前列
     */
    private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {

        //当前
        Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
        //上一行
        Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
        Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();

        //上一行总金额
        Cell preCurDatamoney = cell.getSheet().getRow(curRowIndex-1).getCell(8);
        Object preCurDataMoney = preCurDatamoney.getCellTypeEnum() == CellType.STRING ? preCurDatamoney.getStringCellValue() : preCurDatamoney.getNumericCellValue();
        //当前行总金额
        Cell  curDatacell = writeSheetHolder.getSheet().getRow(curRowIndex).getCell(8);
        Object curDatacellMoney = curDatacell.getCellTypeEnum() == CellType.STRING ? curDatacell.getStringCellValue() : curDatacell.getNumericCellValue();

        //合起来后相加的总rmb
        Double total=Double.parseDouble(String.valueOf(preCurDataMoney))+Double.parseDouble(String.valueOf(curDatacellMoney));

        // 比较当前行指定列的单元格与上一行是否相同，相同合并当前单元格与上一行
        if (curData.equals(preData) && !StringUtils.isEmpty(curData) && !StringUtils.isEmpty(preData)) {
            //todo 名字相等进行总补贴的计算合并操作
            Sheet sheet = writeSheetHolder.getSheet();//表
            List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
            boolean isMerged = false;
            for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
                CellRangeAddress cellRangeAddr = mergeRegions.get(i);
                int firstRow = cellRangeAddr.getFirstRow();
                // 若上一个单元格已经被合并，则先移出原有的合并单元，再重新添加合并单元
                if (cellRangeAddr.isInRange(curRowIndex - 1, 8)) {
                    sheet.removeMergedRegion(i);
                    cellRangeAddr.setLastRow(curRowIndex);
                    sheet.addMergedRegion(cellRangeAddr);
                    for (int j = firstRow; j <= curRowIndex; j++) {
                        sheet.getRow(j).getCell(8).setCellValue(total);
                    }
                    isMerged = true;
                }
            }
            // 若上一个单元格未被合并，则新增合并单元
            if (!isMerged) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex-1, curRowIndex, 8, 8);
                sheet.addMergedRegion(cellRangeAddress);
                sheet.getRow(curRowIndex-1).getCell(8).setCellValue(total);
                sheet.getRow(curRowIndex).getCell(8).setCellValue(total);

            }
        }
    }
}


